﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data.MySqlClient;

namespace l1cs.server.datatables
{
    public class CharacterConfigTable
    {
        private static Logger _log = Logger.getLogger( typeof( CharacterConfigTable ).FullName );

        private static CharacterConfigTable _instance;

        public CharacterConfigTable() { }

        public static CharacterConfigTable getInstance()
        {
            if ( _instance == null )
            {
                _instance = new CharacterConfigTable();
            }
            return _instance;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="objectId"></param>
        /// <param name="length"></param>
        /// <param name="data"></param>
        public void storeCharacterConfig( int objectId, int length, byte[] data )
        {/*TTOK*/
            String sqlstr = @"INSERT INTO character_config SET 
                                object_id = ?1, 
                                length    = ?2, 
                                data      = ?3";
            using ( MySqlWrapper wrapper = new MySqlWrapper() )
            {
                MySqlCommand cmd = new MySqlCommand( sqlstr );
                cmd.Parameters.Add( "?1", MySqlDbType.Int32 ).Value = objectId;
                cmd.Parameters.Add( "?2", MySqlDbType.Int32 ).Value = length;
                cmd.Parameters.Add( "?3", MySqlDbType.Blob ).Value = data;
                wrapper.ExecNonQuery( cmd );
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="objectId"></param>
        /// <param name="length"></param>
        /// <param name="data"></param>
        public void updateCharacterConfig( int objectId, int length, byte[] data )
        {/*TTOK*/
            String sqlstr = @"UPDATE character_config SET 
                               length          = ?1, 
                               data            = ?2
                               WHERE object_id = ?3";
            using ( MySqlWrapper wrapper = new MySqlWrapper() )
            {
                MySqlCommand cmd = new MySqlCommand( sqlstr );
                cmd.Parameters.Add( "?1", MySqlDbType.Int32 ).Value = length;
                cmd.Parameters.Add( "?2", MySqlDbType.Blob ).Value = data;
                cmd.Parameters.Add( "?3", MySqlDbType.Int32 ).Value = objectId;
                wrapper.ExecNonQuery( cmd );
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="objectId"></param>
        public void deleteCharacterConfig( int objectId )
        {/*TTOK*/
            String sqlstr = "DELETE FROM character_config WHERE object_id=?1";
            using ( MySqlWrapper wrapper = new MySqlWrapper() )
            {
                MySqlCommand cmd = new MySqlCommand( sqlstr );
                cmd.Parameters.Add( "?1", MySqlDbType.Int32 ).Value = objectId;
                wrapper.ExecNonQuery( cmd );
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="objectId"></param>
        /// <returns></returns>
        public int countCharacterConfig( int objectId )
        {/*TTOK*/
            int result = 0;
            String sqlstr = @"SELECT count(*) as cnt 
                                FROM character_config 
                                WHERE object_id=?1";
            using ( MySqlWrapper wrapper = new MySqlWrapper() )
            {
                MySqlCommand cmd = new MySqlCommand( sqlstr );
                cmd.Parameters.Add( "?1", MySqlDbType.Int32 ).Value = objectId;
                Object obj = wrapper.ExecScaler( cmd );
                if ( obj != null && !( obj is DBNull ) )
                {
                    result = Convert.ToInt32( obj );
                }
            }
            return result;
        }
    }
}
